package eu.ahsx.limboa.media
{
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	

	/**
	 * 	Data loader
	 * 	Load all the text related data
	 * 
	 * 	@author Masy Alexandre
	 * 	@private
	 * */
	internal class DataLoader extends EventDispatcher implements IAssetsLoader
	{
		private var l:URLLoader;
		
		private var file:MediaFile;
		private var debugging:Boolean;
		
		public function DataLoader(debug:Boolean = false)
		{
			this.debugging = debug;
			super();
		}

		public function load(file:MediaFile):void
		{
			this.file = file;
			
			if (debugging)
				MonsterDebugger.trace("[ASSETS] ", file.id+ " INIT");
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.INIT));
			
			l = new URLLoader();
			l.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
			l.addEventListener(ProgressEvent.PROGRESS, loadProgress, false, 0, true);
			l.addEventListener(IOErrorEvent.IO_ERROR, loadError, false, 0, true);
			l.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError, false, 0, true);
			l.load(new URLRequest(file.url));
			
			if (debugging)
				MonsterDebugger.trace("[ASSETS] ", file.id + " START");
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.START));
		}
		
		public function get percent():int
		{
			if (l)
				return l.bytesLoaded/l.bytesTotal;
			else
				return 0;
		}
		
		public function get data():*
		{
			if (l && l.data)
				return l.data;
			else			
				return null;
		}
		
		public function stop():void
		{
			if (l)
			{
				l.close();
				
				if (debugging)
					MonsterDebugger.trace("[ASSETS] ", file.id + " ERROR " + "Stop the loading");
				
				file.info.error = new ErrorEvent(ErrorEvent.ERROR, false, false, "Stop the loading");
	
				dispatchEvent(new MediaEvent(file, 0, MediaEvent.ERROR));
				
			}
		}
		
		public function unload():void
		{
			if (l)
			{
				l.removeEventListener(Event.COMPLETE, loadComplete);
				l.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
				l.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
				l.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError);
				l = null;
			}
		}
		
		private function loadComplete(event:Event):void
		{
			if (debugging)
				MonsterDebugger.trace("[ASSETS] ", file.id + " COMPLETE");
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.COMPLETE));
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.DONE));
		}
		
		private function loadProgress(event:ProgressEvent):void
		{
			if (debugging)
				MonsterDebugger.trace("[ASSETS] ", file.id + " PROGRESS");
			file.info.bytesloaded = event.bytesLoaded;
			file.info.bytesTotal = event.bytesTotal;
			file.info.percent = file.info.bytesTotal == 0 ? 0 : event.bytesLoaded/event.bytesTotal;
			
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.PROGRESS));
		}
		
		private function loadError(event:ErrorEvent):void
		{
			if (debugging)
				MonsterDebugger.trace("[ASSETS] ", file.id + " ERROR " + event.text);
			
			file.info.error = event;

			dispatchEvent(new MediaEvent(file, 0, MediaEvent.ERROR));
			dispatchEvent(new MediaEvent(file, 0, MediaEvent.DONE));
		}
	}
}